﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace PdfLibrary
{
    public static class ExtensionMethods
    {
        public static string[] ReadLinesToEnd(this StreamReader streamReader)
        {
            var lines = new List<string>();

            string line;
            while ((line = streamReader.ReadLine()) != null)
                lines.Add(line);

            return lines.ToArray();
        }

        public static T ReadAndMove<T>(this IEnumerator<T> enumerator)
        {
            T result = enumerator.Current;
            enumerator.MoveNext();
            return result;
        }

        public static T MoveAndRead<T>(this IEnumerator<T> enumerator)
        {
            enumerator.MoveNext();
            return enumerator.Current;
        }

        public static bool IsInt(string value)
        {
            int tmp;
            return int.TryParse(value, out tmp);
        }

        public static bool IsDouble(string value)
        {
            double tmp;
            return double.TryParse(value, out tmp);
        }

        public static void SetListItemForIndex<T>(List<T> list, int index, T item)
        {
            if(index >= list.Capacity)
                list.Capacity = index + 1;

            while (index >= list.Count)
                list.Add(default(T));

            list[index] = item;
        }

        public static T GetListItemByIndex<T>(List<T> list, int index)
        {
            return index >= list.Count ? default(T) : list[index];
        }
    }
}
